<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>鼠标移入方向</title>
  <style>
    * { margin: 0; padding: 0; box-sizing: border-box;}
    .container {
      margin: 40px;
      width: 200px;
      height: 200px;
      outline: 2px solid;
    }
  </style>
</head>
<body>
  <div class="container">
  </div>
  <script>
    const container = document.querySelector('.container');
    const rect = container.getBoundingClientRect();
    const theta = Math.atan2(rect.height, rect.width);

    container.onmouseenter = (e) => {
      const x = e.offsetX - rect.width / 2;
      const y =  rect.height / 2 - e.offsetY;
      const angle = Math.atan2(y, x); // 判断横坐标和纵坐标的夹角
      if (angle < theta && angle >= -theta) {
        container.classList.add('right');
        console.log('right')
      } else if (angle < -theta && angle >= -Math.PI + theta) {
        container.classList.add('bottom')
        console.log('bottom')
      } else if (angle < Math.PI - theta && angle >= theta) {
        container.classList.add('top')
        console.log('top')
      } else {
        container.classList.add('left')
        console.log('left')
      }
    }
    container.onmouseleave = () => {
      container.className = 'container'
    }
  </script>
</body>
</html>